/*
 * ThermistorTablePlot.hpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   ThermistorTablePlot.hpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   21 jan 2013
 */

#ifndef THERMISTORTABLEPLOT_HPP
#define THERMISTORTABLEPLOT_HPP

#include <vector>
#include <boost/shared_ptr.hpp>

#include "AbstractPlot.hpp"
#include "Thermistor.hpp"
#include "ThermistorTable.hpp"
#include "LeastSquaresSolver.hpp"

using boost::shared_ptr;

namespace tltgen {

  class ThermistorTablePlot : public AbstractPlot {
  public:
    ThermistorTablePlot(const shared_ptr<ThermistorTable>& thermistorTable, const shared_ptr<LeastSquaresSolver>& solver);
    virtual ~ThermistorTablePlot();

    static bool isValidThermistorTable(const shared_ptr<ThermistorTable>& thermistorTable);
    static bool isValidLeastSquaresSolver(const shared_ptr<LeastSquaresSolver>& solver);

    virtual bool isValidThermistorModel(const shared_ptr<ThermistorModel>& thermistorModel) const;
    virtual void addThermistorModel(const shared_ptr<ThermistorModel>& thermistorModel);
    virtual void removeThermistorModel(const shared_ptr<ThermistorModel const>& thermistorModel);

    virtual shared_ptr<ThermistorTable> getThermistorTable() const;
    virtual shared_ptr<LeastSquaresSolver> getLeastSquaresSolver() const;

    virtual unsigned int getNbDatasets() const;  
    virtual std::string getDatasetName(unsigned int i) const;

    virtual void plotViewport(wxPLplotstream *pls);
    virtual void plotDataset(wxPLplotstream *pls, unsigned int i);

    virtual std::string getXLabel() const = 0;
    virtual std::string getYLabel() const = 0;

    virtual unsigned int getXMaxDigits() const = 0;
    virtual unsigned int getYMaxDigits() const = 0;

    virtual tuple<PLFLT, PLFLT> getXBoundsAuto() const;
    virtual tuple<PLFLT, PLFLT> getYBoundsAuto() const;

    virtual bool isValidRow(ThermistorTableRow* thermistorTableRow) const = 0;
    virtual PLFLT getXValue(ThermistorTableRow* thermistorTableRow) const = 0;
    virtual PLFLT getYValue(ThermistorTableRow* thermistorTableRow) const = 0;
    virtual PLFLT getYValue(const shared_ptr<Thermistor>& thermistor, PLFLT xValue) const = 0;
    virtual PLFLT getYValue(unsigned int i, PLFLT x) const;

  protected:
    static bool compareFirst(tuple<PLFLT,PLFLT> t1, tuple<PLFLT,PLFLT> t2);
    virtual void sort(PLFLT x[], PLFLT y[], size_t np) const;

    virtual void plotTableDataset(wxPLplotstream *pls);
    virtual void plotThermistorModel(wxPLplotstream *pls, const shared_ptr<ThermistorModel>& thermistorModel);

    virtual void thermistorTableUpdated();

    virtual void onThermistorTableChanged(ThermistorTable::Event e, ThermistorTableRow *tableRow);

    shared_ptr<ThermistorTable> thermistorTable;
    std::vector<shared_ptr<ThermistorModel> > thermistorModels;
    shared_ptr<LeastSquaresSolver> solver;
  };

}

#endif
